package cn.genmer.test.security.algorithm;

import cn.genmer.test.security.algorithm.sort.QuickSort;
import cn.genmer.test.security.algorithm.sort.SelectSort;
import cn.genmer.test.security.utils.SystemUsageUtil;

import java.util.Arrays;
import java.util.Random;

/**
 * 排序算法对数器
 */
public class SortDataChecker {
    public static void main(String[] args) {
        check(1000, 1000, 1);
    }

    public static void check(int arraySize, int CycleNum, int mode){
        // mode: 0. 调试模式 | 1. 测试模式

        // 新增消耗时间、消耗内存显示
        SystemUsageUtil systemUsageUtil = new SystemUsageUtil();
        systemUsageUtil.markingTimeConsume();
        systemUsageUtil.markingMemoryConsume();

        boolean result = true;
        for (int k=0; k<CycleNum; k++){
            int[] arr = generateRandomArray(arraySize, mode);
            int[] arr2 = Arrays.copyOf(arr, arr.length);
            int[] originArr = Arrays.copyOf(arr, arr.length);

            Arrays.sort(arr);
            QuickSort.sortTEST(arr2, 0, arr2.length-1);
//            MergeSort.sortTEST(arr2, 0, arr2.length-1);
//            ShellSort.sortTest(arr2);
//            InsertSort.sortTEST(arr2);
//            SelectSort.sortTEST(arr2);
//            BubbleSort.sortTEST(arr2);
//            arr2 = CountSort.sortTEST(arr2);
//            RadixSort.sortTEST(arr2);
//            System.out.println("out arr2" + Arrays.toString(arr2));

            boolean same = true;
            for(int i=0; i<arr.length; i++){
                if (arr[i] != arr2[i]) same = false;
            }
            if (!same) {
                result = false;
                System.out.println("error");
                System.out.println(String.format("执行第%s次出错", k+1));
                System.out.print("测试用例: ");
                SelectSort.printArray(originArr);
                System.out.println();
                System.out.print("正确用例: ");
                SelectSort.printArray(arr);
                System.out.println();
                System.out.print("当前用例: ");
                SelectSort.printArray(arr2);
                return;
            }
        }
        if (result)
            System.out.println("right");

        systemUsageUtil.markingTimeConsume();
        systemUsageUtil.markingMemoryConsume();
    }

    /**
     * 生成随机数组
     * @return
     */
    private static int[] generateRandomArray(int size, int mode){
        Random random = new Random();
        int length = mode == 0 ? 3 : size;
        int[] randoms = new int[length];
        for (int i=0; i< randoms.length; i++){
            randoms[i] = random.nextInt(randoms.length);
        }
        return randoms;
    }
}
